import { Base64 } from "js-base64";

export const seeFile = (fileId: string, fileName: string) => {
  let gateway = "http://" + window.location.host;
  if (import.meta.env.MODE === "dev") {
    gateway = APP_CONFIG.serverUrl;
  }
  let format = fileName.split(".")[1];
  fileName = fileId + "." + format;

  let originurl = `${APP_CONFIG.frsUrl}${APP_CONFIG.frsServer}/api/file/download/${fileId}`;
  if (APP_CONFIG.isSpePicPreview) {
    let arr = ["gif", "jpeg", "jpg", "png", "bmp", "ico"];
    if (arr.indexOf(format) !== -1) {
      originurl = `${gateway}${APP_CONFIG.frsServer}/api/file/download/${fileId}`;
    }
  }

  let previewUrl = `${originurl}?fullfilename=${fileName}`;
  let url = "";
  let url2 = "";
  //是否使用base64
  if (APP_CONFIG.openFileBase64) {
    url = encodeURIComponent(Base64.encode(previewUrl));
    url2 = encodeURIComponent(Base64.encode(`${gateway}/kk-file/`));
  } else {
    url = encodeURIComponent(previewUrl);
    url2 = encodeURIComponent(`${gateway}/kk-file/`);
  }
  //图片预览是否特殊处理
  window.open(`${gateway}/kk-file/onlinePreview?url=${url}&customBaseUrl=${url2}`, "_blank");
};

export const downFile = (fileId: string) => {
  let gateway = "http://" + window.location.host;
  let originurl = `${gateway}${APP_CONFIG.frsServer}/api/file/download/${fileId}`;
  window.open(originurl, "_blank");
};
